var Common = require('wy-Common')
var PathActions = require('PathActions')
var FrameManager = require('FrameManager')
cc.Class({
    extends: require('FrameComponent'),

    properties: {
        asset: cc.RawAsset,
        target: cc.Node,
        autoPlay: true,
        autoEvent: {
            default: [],
            type: cc.Component.EventHandler,
            tooltip: "i18n:COMPONENT.button.click_events"
        },
        n_currentTime: 0,
        n_step: 0
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.loadend = false;
        this.c = 0;
        if (this.asset) {
            var self = this;
            cc.loader.load(this.asset, function (err, res) {
                self.loadData(JSON.parse(res))
                self.loadend = true;
                if (self.autoPlay) {
                    self.runAction(1)
                }
            });
        }
        // this.flipx = true;
        // this.flipy = true;
    },

    loadData(datas) {
        this.loadend = true;
        if(!this.datas || this.datas.name != datas.name){
            this.datas = Common.deepCopy(datas);
        }
        if (datas.points.length < 2) {
            console.warn('格式不对')
            return;
        }
        this.target.stopAllActions();
    },

    getStepTime() {
        this.times = [];
        let totleTime = 0;
        for (let i = 0; i < this.datas.datas.length; i++) {
            if (this.datas.datas[i] && this.datas.datas[i].time) {
                totleTime = totleTime + this.datas.datas[i].time;
                let obj = {
                    time: totleTime,
                    center: this.datas.datas[i].center,
                    shun: this.datas.datas[i].shun,
                }
                this.times.push(obj);
            }
        }
    },

    initPosition() {
        this.initPosition = this.node.getPosition();
    },

    setFlip(x, y) {
        this.flipx = !!x;
        this.flipy = !!y;
    },

    getFlipVec(center, p) {
        var x = (p.x - center.x);
        if (this.flipx) {
            x = center.x * 2 - p.x
        }
        var y = (p.y - center.y)
        if (this.flipy) {
            y = center.y * 2 - p.y
        }
        return cc.v2(x, y)
    },

    getFlipData() {
        var first = this.datas.points[0];

        var datas = {
            points: [],
            datas: []
        }
        for (var i = 0; i < this.datas.datas.length; i++) {
            var data = this.datas.datas[i];

            var p = cc.v2(this.datas.points[i].x - first.x, this.datas.points[i].y - first.y)
            this.flipx && (p.x *= -1)
            this.flipy && (p.y *= -1)
            // var p = this.getFlipVec(first,this.datas.points[i]);
            datas.points.push(p)

            if (data) {
                var dp = Common.deepCopy(data);
                if (data.action == 1) {
                    // var x = (data.center.x-first.x) * (this.flipx ? -1 : 1)
                    // var y = (data.center.y-first.y) * (this.flipy ? -1 : 1)
                    dp.center = cc.v2(data.center.x - first.x, data.center.y - first.y)
                    this.flipx && (data.center.x *= -1)
                    this.flipy && (data.center.y *= -1)
                    if (this.flipx && this.flipy) {
                        dp.start = +Math.PI + data.start;
                        dp.end = +Math.PI + data.end;
                    } else if (this.flipx) {
                        dp.shun = !data.shun
                        dp.start = Math.PI - data.start;
                        dp.end = Math.PI - data.end;
                    } else if (this.flipy) {
                        dp.shun = !data.shun
                        dp.end *= -1
                        dp.start *= -1
                    }
                }

                datas.datas[i] = dp
            }

        }
        return datas;
    },

    runAction(repeat, cb) {
        var datas = this.getFlipData();
        if (!this.initPosition) {
            this.initPosition = this.datas.points[0];
        } else {
            this.initPosition = this.node.getPosition();
        }
        var run = this.target.getNumberOfRunningActions();
        var first = this.datas.points[0];
        // cc.log('运行位置')
        // var first = cc.v2(f.x * (this.flipx?-1:1),f.y * (this.flipy?-1:1))

        var points = datas.points;
        this.actions = new PathActions(this.target);
        var actions = [];
        for (var i = 1; i < datas.datas.length; i++) {
            var data = datas.datas[i];
            if (data.delay > 0) {
                this.actions.addDelayTime(data.delay);
                // actions.push(cc.delayTime(data.delay));
            }

            if (data.action == 1) {
                this.actions.addCircle(data.time, data.center.add(this.initPosition), data.radius, data.start + 0.5 * Math.PI, data.end + 0.5 * Math.PI, !data.shun)
                // actions.push(cc.circle(data.time, data.center.add(this.initPosition), data.radius, data.start + 0.5 * Math.PI, data.end + 0.5 * Math.PI, !data.shun))
            } else {
                this.actions.addMoveTo(data.time,points[i].add(this.initPosition))
                // actions.push(cc.moveTo(data.time, points[i].add(this.initPosition)));
            }
        }
        var self = this;
        if (actions) {
            // this.actions.addCallBack(function(){
            //     self.target.setPosition(self.initPosition)
            // })
        }
        if (repeat == -1) {
            this.target.runAction(cc.repeatForever(cc.sequence(actions)));
        } else {
            repeat = !repeat ? 0 : 1
            if (cb || this.autoEvent.length > 0) {
                this.actions.addCallBack(function(){
                    cc.log('addCallBack')
                    cb && cb();
                    cc.Component.EventHandler.emitEvents(self.autoEvent, self);
                    self.actions = null;
                })
            }
            // this.target.runAction(cc.repeat(cc.sequence(actions), 1));
        }
        this.actions.start(repeat);
    },
    update(dt){
        this.autoUpdate && this.actions && this.actions.step(dt);
    },
    gameUpdate(dt){
        this.actions && this.actions.step(dt);
    }

});
